package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("添加菜品")
    public Result addDish(@RequestBody DishDTO dishDTO) {
        log.info("添加菜品:{}", dishDTO);
        dishService.DishWishFlavor(dishDTO);

        //清理缓存数据
        String key = "dish_" + dishDTO.getCategoryId();
        redisTemplate.delete(key);
        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @param queryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO queryDTO) {
        log.info("菜品分页查询：{}", queryDTO);
        PageResult result = dishService.page(queryDTO);
        return Result.success(result);
    }

    /**
     * 菜品的批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("菜品的批量删除")
    public Result delBatch(@RequestParam List<Long> ids) {//@RequestParam会自动解析穿过来的1,2,3 自动解析并传入集合
        log.info("菜品的批量删除:{}", ids);
        dishService.delBatch(ids);

        //清理所有的菜品缓存(dish_*)
        ClearCache("dish_");
        return Result.success();
    }

    /**
     * 根据id查询菜品信息和对应的口味数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品信息和对应的口味数据")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id:{}查询菜品信息和对应的口味数据", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品id的信息和对应的口味
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品id的信息和对应的口味")
    public Result UpdateDish(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品id的信息和对应的口味:{}", dishDTO);
        dishService.updateByIdWithFlavor(dishDTO);

        //清理所有的菜品缓存(dish_*)
        ClearCache("dish_");
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> GetDishsByCategoryId(Long categoryId) {
        log.info("根据分类id查询菜品");
        List<Dish> dishes = dishService.GetDishsByCategoryId(categoryId);
        return Result.success(dishes);
    }

    /**
     * 修改菜品状态
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    public Result UpdateDishStatus(@PathVariable Integer status, Long id) {
        log.info("修改菜品状态");
        dishService.UpdateDishStatus(status, id);

        //清理所有的菜品缓存(dish_*)
        ClearCache("dish_");
        return Result.success();
    }

    /**
     * 清理所有缓存数据
     */
    private void ClearCache(String pattern) {
        //清理所有的菜品缓存(dish_*)
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
